package com.gexing.jsondemo.ui;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtils {
	public static final String CLASSNAME_STRING = "java.lang.String";
	public static final String CLASSNAME_INT = "int";
	public static final String CLASSNAME_LONG = "long";
	public static final String CLASSNAME_BYTE = "byte";
	public static final String CLASSNAME_CHAR = "char";
	public static final String CLASSNAME_DOUBLE = "double";
	public static final String CLASSNAME_FLOAT = "float";
	public static final String CLASSNAME_BOOLEAN = "boolean";
	public static final String CLASSNAME_ARRAY = "java.util.ArrayList";

	/**
	 * 判断给定字符串是否有内容。
	 * 
	 * @param str
	 * @return 如果字符串为空字符串或者null则返回false，其他情况返回true。
	 */
	public static boolean isNotBlank(String str) {
		if (str == null) {
			return false;
		} else if ("".equals(str.trim())) {
			return false;
		}
		return true;
	}

	public static boolean isNotBlank(String... strings) {
		for (String str : strings) {
			if (str == null) {
				return false;
			} else if ("".equals(str.trim())) {
				return false;
			}
		}
		return true;

	}

	public static int index = -1;

	public static ArrayList<String> getNewArray(ArrayList<String> array, int end) {
		ArrayList<String> newArray = new ArrayList<String>();

		for (int i = 0; i < array.size(); i++) {
			String str = array.get(i);
			if (FaceUtils.hasFace(str)) {
				newArray.add(str);
			} else {
				if ("[".equals(str)) {
					String str1 = "[";
					if (i == array.size() - 1) {
						newArray.add(str);
					}
					for (int j = i + 1; j < array.size(); j++) {
						String string = array.get(j);
						str1 += string;
						if ("]".equals(string)) {
							if (FaceUtils.hasFace(str1)) {
								newArray.add(str1);
								i = j;
								if (i > end) {
									index = newArray.size();
								}
							} else {
								newArray.add(str);
							}
							break;
						}
						if (j == array.size() - 1) {
							newArray.add(str);
						}
					}
				} else {
					newArray.add(str);
				}
			}
			if (i == end) {
				index = newArray.size();
			}
		}
		array.clear();
		array = null;
		return newArray;
	}

	public static String stringArrayToHtml(ArrayList<String> array) {
		String str = "";
		for (String string : array) {
			string = FaceUtils.getFace(string);
			str += string;
		}
		return str;
	}

	public static String stringArrayToString(ArrayList<String> array) {
		String str = "";
		for (String string : array) {
			str += string;
		}
		return str;
	}

	public static String getGenericsType(String type) {
		return type.substring(type.indexOf("<")+1,type.indexOf(">"));
	}

	public static String setColor(String str, String color) {
		return "<font color='#"+color+"'>"+str+"</font>";
	}

	public static boolean isBaseDataType(String dataType) {
		if(CLASSNAME_BOOLEAN.equals(dataType)){
			return true;
		}else if(CLASSNAME_INT.equals(dataType)){
			return true;
		}else if(CLASSNAME_LONG.equals(dataType)){
			return true;
		}else if(CLASSNAME_STRING.equals(dataType)){
			return true;
		}else if(CLASSNAME_BYTE.equals(dataType)){
			return true;
		}else if(CLASSNAME_CHAR.equals(dataType)){
			return true;
		}else if(CLASSNAME_DOUBLE.equals(dataType)){
			return true;
		}else if(CLASSNAME_FLOAT.equals(dataType)){
			return true;
		}else{
			return false;
		}
	}
	
	public static boolean getEmail(String line){  
        Pattern p = Pattern.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");  
        Matcher m = p.matcher(line);  
        return m.find();  
    }

	public static boolean checkStringLengthRange(String str, int minLen, int maxLen) {
		if(str.length()<minLen||str.length()>maxLen){
			return false;
		}
		return true;
	}  
	
	public static float getTextCount(String text){
		float chineseCount = (text.getBytes().length - text.length()) / 2;
		return chineseCount + (text.length() - chineseCount) / 2;
	}
	
	public static String getSubStringIfCountOut(String string, int flagNum) {
		String subString="";
		String sub="";
		float count4=0;
		float needCount=flagNum;
		int startIndex=0;
		int endIndex=flagNum;
		while (count4 < flagNum) {
			sub = string.substring(startIndex, endIndex);
			float textCount3 = StringUtils.getTextCount(sub);
			if (textCount3 > needCount) {
				subString+=sub.substring(0,Math.round(needCount)-1);
				break;
			} else if (textCount3 == needCount) {
				subString += sub;
				count4 += textCount3;
				needCount = flagNum - count4;
			} else {
				subString += sub;
				count4 += textCount3;
				needCount = flagNum - count4;
				startIndex = endIndex;
				endIndex = startIndex + Math.round(needCount);
			}
		}
		return subString;
	}
}
